package com.xywzxw.niushop.utils;


import com.lop.open.api.sdk.DefaultDomainApiClient;
import com.lop.open.api.sdk.LopException;
import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonCargoInfo;
import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonCreateOrderRequest;
import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.CommonProductInfo;
import com.lop.open.api.sdk.domain.ECAP.CommonCreateOrderApi.commonCreateOrderV1.Contact;
import com.lop.open.api.sdk.domain.ECAP.CommonQueryOrderApi.commonGetOrderTraceV1.CommonOrderTraceRequest;
import com.lop.open.api.sdk.plugin.LopPlugin;
import com.lop.open.api.sdk.plugin.factory.OAuth2PluginFactory;
import com.lop.open.api.sdk.request.ECAP.EcapV1OrdersCreateLopRequest;
import com.lop.open.api.sdk.request.ECAP.EcapV1OrdersTraceQueryLopRequest;
import com.lop.open.api.sdk.response.ECAP.EcapV1OrdersCreateLopResponse;
import com.lop.open.api.sdk.response.ECAP.EcapV1OrdersTraceQueryLopResponse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class JDExpressTools {

    static Boolean envIsProd = true;
    static String prodUrl = "https://api.jdl.com";
    static String devUrl = "https://uat-api.jdl.com";
    //DefaultDomainApiClient对象全局只需要创建一次
    static public DefaultDomainApiClient client = new DefaultDomainApiClient(envIsProd ? prodUrl : devUrl, 500, 15000);

    public EcapV1OrdersTraceQueryLopResponse getOrderTrace(String appKey, String appSecret, String accessToken, String waybillCode) throws LopException {
        EcapV1OrdersTraceQueryLopRequest request = new EcapV1OrdersTraceQueryLopRequest();
        CommonOrderTraceRequest commonOrderTraceRequest = new CommonOrderTraceRequest();
        commonOrderTraceRequest.setWaybillCode(waybillCode);
        request.setCommonOrderTraceRequest(commonOrderTraceRequest);

        LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(appKey, appSecret, accessToken);
        request.addLopPlugin(lopPlugin);
        EcapV1OrdersTraceQueryLopResponse response = client.execute(request);
        return response;
    }

    public EcapV1OrdersCreateLopResponse createOrder(JDExpressModel model) {
        //示例为调用京东快递下单前置校验接口
        try {

            //入参对象（请记得更换为自己要使用的接口入参对象）
            EcapV1OrdersCreateLopRequest request = new EcapV1OrdersCreateLopRequest();


            //设置入参（请记得更换为自己要使用的接口入参）
            Contact senderContact = new Contact();
            senderContact.setFullAddress(model.getSendAddress());
            senderContact.setName(model.getSendUser());
            senderContact.setMobile(model.getSendMobile());

            Contact receiverContact = new Contact();
            receiverContact.setFullAddress(model.getReceiveAddress());
            receiverContact.setName(model.getReceiveUser());
            receiverContact.setMobile(model.getReceiveMobile());

            CommonCreateOrderRequest requestDTO = new CommonCreateOrderRequest();
            if (model.getPickupStartTime() != null && model.getPickupStartTime() != 0) {
                requestDTO.setPickupStartTime(model.getPickupStartTime());
            }
            if (model.getPickupEndTime() != null && model.getPickupEndTime() != 0) {
                requestDTO.setPickupEndTime(model.getPickupEndTime());
            }
            requestDTO.setOrderOrigin(1);
            requestDTO.setOrderId(model.getOrderId());
            requestDTO.setSettleType(3);
            requestDTO.setCustomerCode(model.getCustomCode());
            CommonProductInfo productInfo = new CommonProductInfo();
            productInfo.setProductCode("ed-m-0001");
            requestDTO.setProductsReq(productInfo);
            requestDTO.setReceiverContact(receiverContact);
            requestDTO.setSenderContact(senderContact);

            List<CommonCargoInfo> list = new ArrayList<CommonCargoInfo>();
            CommonCargoInfo commonCargoInfo = new CommonCargoInfo();
            commonCargoInfo.setVolume(BigDecimal.valueOf(model.getVolume()));
            commonCargoInfo.setWeight(BigDecimal.valueOf(model.getWeight()));
            commonCargoInfo.setQuantity(1);
            commonCargoInfo.setName("衣服");
            list.add(commonCargoInfo);
            requestDTO.setCargoes(list);
            request.setRequest(requestDTO);


            LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(model.getAppKey(), model.getAppSecret(), model.getAccessToken());
            request.addLopPlugin(lopPlugin);
            EcapV1OrdersCreateLopResponse response = client.execute(request);

            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
